home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / HENSA / MATHS / PLPLOT / PLPLOT.ZIP / include / tclMatrix. < prev   
Encoding:
Text File  |  1994-08-11  |  7.2 KB  |  278 lines

  1. /* -*-C++-*-
  2.  * $Id: tclMatrix.h,v 1.9 1994/08/11 01:43:07 furnish Exp $
  3.  * $Log: tclMatrix.h,v $
  4.  * Revision 1.9  1994/08/11  01:43:07  furnish
  5.  * Implement redim methods for the C++ shadow class.  Somebody still
  6.  * needs to do it in the Tcl code :-).
  7.  *
  8.  * Revision 1.8  1994/08/05  17:45:31  furnish
  9.  * More on the C++ shadow class.
  10.  *
  11.  * Revision 1.7  1994/06/30  18:38:49  mjl
  12.  * Changed typedef for Mat_int back to an int, and eliminated M?D macros in
  13.  * favor of directly addressing the array.
  14.  *
  15.  * Revision 1.6  1994/06/30  05:45:21  furnish
  16.  * Cobbled together an extension facility which allows a user to define
  17.  * their own subcommands for tclMatricies.  The idea is that you can use
  18.  * this to implement your own matrix processing commands entirely on the
  19.  * compiled side.  fft's, matrix multiplication, inversion, etc.
  20.  *
  21.  * Revision 1.5  1994/06/26  05:16:15  furnish
  22.  * Implemented C++ wrapper class for tclMatrix, enabling easy use of a
  23.  * tclMatrix from compiled extension commands.  Needs to have sibling
  24.  * classes created by someone who cares.
  25.  *
  26.  * Revision 1.4  1994/06/25  20:35:49  mjl
  27.  * Changed typedef for Mat_int to long.  Maybe I should have a Mat_long
  28.  * instead?  Added put/get function handlers to matrix struct (determined
  29.  * when created, dependent on type).
  30.  *
  31.  * Revision 1.3  1994/06/24  20:37:55  mjl
  32.  * Changed name of struct to tclMatrix to avoid conflicts with C++ Matrix
  33.  * classes.  Put in ifdef-ed extern "C" linkage statements for C++.
  34.  *
  35.  * Revision 1.2  1994/06/16  21:57:11  mjl
  36.  * Added matrix operator name to command data struct.
  37.  *
  38.  * Revision 1.1  1994/06/16  19:49:05  mjl
  39.  * Header file for new Tcl matrix command.
  40.  */
  41.  
  42. /*----------------------------------------------------------------------*\
  43.  *
  44.  * tclMatrix.h --
  45.  *
  46.  *    Contains declarations for Tcl "Matrix" command.
  47.  *    C functions that need access to the matrix data will need
  48.  *    to include this file.
  49.  *
  50.  * Maurice LeBrun
  51.  * IFS, University of Texas at Austin
  52.  * 13-Jun-1994
  53. \*----------------------------------------------------------------------*/
  54.  
  55. #ifndef __TCLMATRIX_H__
  56. #define __TCLMATRIX_H__
  57.  
  58. #ifdef DOUBLE
  59. typedef double Mat_float;
  60. #else
  61. typedef float  Mat_float;
  62. #endif
  63.  
  64. #if defined(MSDOS)
  65. typedef long  Mat_int;
  66. #else
  67. typedef int   Mat_int;
  68. #endif
  69.  
  70. enum { TYPE_FLOAT, TYPE_INT };
  71.  
  72. /* Arrays are column dominant (normal C ordering) */
  73. /* Array elements are stored contiguously */
  74. /* Require dimension <= 3, floats for simplicity */
  75.  
  76. #define MAX_ARRAY_DIM 3
  77.  
  78. /* Useful macros for index calculations */
  79.  
  80. #define I3D(i,j,k)    k + matPtr->n[2] * (I2D(i,j))
  81. #define I2D(i,j)    j + matPtr->n[1] * (I1D(i))
  82. #define I1D(i)        i
  83.  
  84. /* Matrix operator data */
  85.  
  86. typedef struct {
  87.     int type;            /* Data type */
  88.                 /* For now only float/double are supported */
  89.     int len;            /* Total length of array */
  90.     int dim;            /* Number of dimensions */
  91.     int n[MAX_ARRAY_DIM];    /* Holds array length in each dimension */
  92.     char name[20];        /* Matrix operator name */
  93.  
  94.     Mat_float *fdata;        /* Floating point data */
  95.     Mat_int   *idata;        /* Integer data */
  96.  
  97. /* These do the put/get operations for each supported type */
  98.  
  99.     void (*put) (ClientData clientData, int index, char *string);
  100.     void (*get) (ClientData clientData, int index, char *string);
  101.  
  102. } tclMatrix;
  103.  
  104. /* Function prototypes */
  105.  
  106. #ifdef __cplusplus
  107. /*---------------------------------------------------------------------------//
  108. // Since C++ does not generally have a per-platform ABI the way C
  109. // does, we stick to a totally inline class declaration and
  110. // definition.  That way you don't have to keep a separate version of
  111. // libplplot*.a for each compiler you'd like to use.
  112.  
  113. // Start by setting up some important macros.
  114.  
  115. // Okay, HP C++ has exceptions, but most other compilers don't.  Will
  116. // embellish this as necessary.
  117. */
  118.  
  119. #ifdef throw
  120. #define TCL_NO_UNDEF
  121. #endif
  122.  
  123. #ifndef throw
  124. #ifdef __hpux
  125. #if defined(__GCC__) || defined(__lucid) || defined(__Centerline)
  126. #define NO_XCPT
  127. #endif
  128. #else
  129. #define NO_XCPT
  130. #endif
  131.  
  132. #ifdef NO_XCPT
  133. #define try
  134. #define throw(a) \
  135. { cerr << "THROW: " << #a << " from " << __FILE__ \
  136.        << " line " << __LINE__ << endl << flush; abort(); }
  137. #define catch(a) if (0)
  138. #define Throw
  139. #else
  140. #define Throw throw
  141. #endif
  142. #endif
  143.  
  144. #define tMat_Assert(a,b) if (!(a)) \
  145. { cerr << "Assertion " << #a << " failed in " << __FILE__ \
  146.        << " at line " << __LINE__ << endl << flush; \
  147.   throw(b); }
  148.  
  149. /*---------------------------------------------------------------------------//
  150. // class TclMatFloat
  151.  
  152. // This class provides a convenient way to access the data of a
  153. // tclMatrix from within compiled code.  Someone should make clones of
  154. // this class for the other tclMatrix supported data types.
  155. //---------------------------------------------------------------------------*/
  156.  
  157. class TclMatFloat {
  158.     tclMatrix *matPtr;
  159.   public:
  160.     TclMatFloat( tclMatrix *ptm )
  161.     : matPtr(ptm)
  162.     {
  163.     tMat_Assert( matPtr->type == TYPE_FLOAT, "Type mismatch" );
  164.     }
  165.  
  166.     int Dimensions() { return matPtr->dim; }
  167.  
  168.     void redim( int nx )
  169.     {
  170.     free( matPtr->fdata );
  171.     matPtr->dim = 1;
  172.     matPtr->n[0] = nx;
  173.     matPtr->len = nx;
  174.     matPtr->fdata = (float *) malloc( matPtr->len * sizeof(float) );
  175.     }
  176.  
  177.     void redim( int nx, int ny )
  178.     {
  179.     free( matPtr->fdata );
  180.     matPtr->dim = 2;
  181.     matPtr->n[0] = nx;
  182.     matPtr->n[1] = ny;
  183.     matPtr->len = nx * ny;
  184.     matPtr->fdata = (float *) malloc( matPtr->len * sizeof(float) );
  185.     }
  186.  
  187.     void redim( int nx, int ny, int nz )
  188.     {
  189.     free( matPtr->fdata );
  190.     matPtr->dim = 3;
  191.     matPtr->n[0] = nx;
  192.     matPtr->n[1] = ny;
  193.     matPtr->n[2] = nz;
  194.     matPtr->len = nx * ny * nz;
  195.     matPtr->fdata = (float *) malloc( matPtr->len * sizeof(float) );
  196.     }
  197.  
  198.     Mat_float& operator()( int i )
  199.     {
  200.     tMat_Assert( matPtr->dim == 1, "Wrong number of indicies." );
  201.     tMat_Assert( i >= 0 && i < matPtr->n[0],
  202.              "Out of bounds reference" );
  203.  
  204.     return matPtr->fdata[i];
  205.     }
  206.  
  207.     Mat_float& operator()( int i, int j )
  208.     {
  209.     tMat_Assert( matPtr->dim == 2, "Wrong number of indicies." );
  210.     tMat_Assert( i >= 0 && i < matPtr->n[0] &&
  211.             j >= 0 && j < matPtr->n[1],
  212.             "Out of bounds reference" );
  213.         
  214.     return matPtr->fdata[I2D(i,j)];
  215.     }
  216.  
  217.     Mat_float& operator()( int i, int j, int k )
  218.     {
  219.     tMat_Assert( matPtr->dim = 3, "Wrong number of indicies." );
  220.     tMat_Assert( i >= 0 && i < matPtr->n[0] &&
  221.             j >= 0 && j < matPtr->n[1] &&
  222.             k >= 0 && k < matPtr->n[2],
  223.             "Out of bounds reference" );
  224.  
  225.     return matPtr->fdata[I3D(i,j,k)];
  226.     }
  227. };
  228.  
  229. #ifndef TCL_NO_UNDEF
  230.  
  231. #ifdef NO_XCPT
  232. #undef NO_XCPT
  233. #undef try
  234. #undef throw
  235. #undef Throw
  236. #undef catch
  237. #endif
  238.  
  239. #endif
  240.  
  241. #undef tMat_Assert
  242.  
  243. extern "C" {
  244. /*---------------------------------------------------------------------------*/
  245. #endif
  246.  
  247. #include <tcl.h>
  248.  
  249. /* This procedure is invoked to process the "matrix" Tcl command. */
  250.  
  251. int
  252. Tcl_MatrixCmd(ClientData clientData, Tcl_Interp *interp,
  253.           int argc, char **argv);
  254.  
  255. /* Returns a pointer to the specified matrix operator's data. */
  256.  
  257. tclMatrix *
  258. Tcl_GetMatrixPtr(Tcl_Interp *interp, char *matName);
  259.  
  260. /* Some stuff for handling extension subcommands. */
  261.  
  262. typedef int (*tclMatrixXtnsnProc) ( tclMatrix *pm, Tcl_Interp *interp,
  263.                     int argc, char *argv[] );
  264.  
  265. typedef struct tclMatrixXtnsnDescr {
  266.     char *cmd;
  267.     tclMatrixXtnsnProc cmdproc;
  268.     struct tclMatrixXtnsnDescr *next;
  269. } tclMatrixXtnsnDescr;
  270.  
  271. int Tcl_MatrixInstallXtnsn( char *cmd, tclMatrixXtnsnProc proc );
  272.  
  273. #ifdef __cplusplus
  274. }
  275. #endif
  276.  
  277. #endif    /* __TCLMATRIX_H__ */
  278.